﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using System.Reflection;

namespace BLLA
{
    public abstract class BaseBLL<T>:IBaseBLL<T> where T:class,new()
    {
        public BaseBLL()
        {
            SetDAL();
        }
        /// <summary>
        /// 由子类实现，为 业务父类 里的 数据接口对象 设置 值！
        /// </summary>
        public abstract void SetDAL();
        //1.数据层接口 对象 - 等待 被实例化
        protected IDAL.IBaseDAL<T> idal;// = new idal.BaseDAL();
        protected IDAL.IDBSession iDbSession;
        #region 数据仓储 属性 + IDBSession DBSession
        public IDAL.IDBSession DBSession
        {
            get
            {

                if (iDbSession == null)
                {
                    //这里可以用Spring.net优化

                    ////1.读取配置文件
                    //string strFactoryDLL = Common.ConfigurationHelper.AppSetting("DBSessionFatoryDLL");
                    //string strFactoryType = Common.ConfigurationHelper.AppSetting("DBSessionFatory");
                    ////2.1通过反射创建 DBSessionFactory 工厂对象
                    //Assembly dalDLL = Assembly.LoadFrom(strFactoryDLL);//根据路径找到程序集读到程序
                    //Type typeDBSessionFatory = dalDLL.GetType(strFactoryType);
                    ////根据实例创建工厂
                    ////IDAL.IDBSessionFactory sessionFactory = Activator.CreateInstance(typeDBSessionFatory) as IDAL.IDBSessionFactory;
                    ////2.根据配置文件内容 使用 DI层里的Spring.Net 创建 DBSessionFactory 工厂对象
                    //Spring.net反射获得数据存储工厂
                    IDAL.IDBSessionFactory sessionFactory = DI.SpringHelper.GetObject<IDAL.IDBSessionFactory>("DBSessFactory");
                    //3.通过 工厂 创建 DBSession对象
                    iDbSession = sessionFactory.GetDBSession();
                }
                return iDbSession;
            }
        }
        #endregion

        public int Add(T model)
        {
            
            return idal.Add(model);
           
        }

        public int Del(T model)
        {
            return idal.Del(model);
        }

        public int DelBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere)
        {
            return DelBy(delWhere);
        }

        public int Modify(T model, params string[] proNames)
        {
            throw new NotImplementedException();
        }

        public int ModifyBy(T model, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
        {
            throw new NotImplementedException();
        }

        public List<T> GetListBy(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListBy(whereLambda);
        }

        public List<T> GetListBy<TKey>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> orderLambda)
        {
            throw new NotImplementedException();
        }

        public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey>> orderBy)
        {
            throw new NotImplementedException();
        }
    }
}
